package com.google.appengine.api.users;

import lombok.Data;
import org.apache.hadoop.hbase.util.Bytes;

/**
 *
 * @author Secondary User
 */
@Data
public class User
{

	private final String email;
	private final String authDomain;
	private final String userId;
	private final static String UGLY_SEPARATOR = "#ob~";

	public User(String email, String authDomain)
	{
		this(email, authDomain, email.split("@")[0]);
	}

	public User(String email, String authDomain, String userId)
	{
		this.email = email;
		this.authDomain = authDomain;
		this.userId = userId;
	}

	public static User getUser(String ugly)
	{
		if (ugly == null)
		{
			return null;
		}
		String email;
		String authDomain;
		String userId;
		int loc = ugly.indexOf(UGLY_SEPARATOR);
		if (loc >= 0)
		{
			email = ugly.substring(0, loc);
			ugly = ugly.substring(loc + UGLY_SEPARATOR.length());
			loc = ugly.indexOf(UGLY_SEPARATOR);
			if (loc >= 0)
			{
				authDomain = ugly.substring(0, loc);
				userId = ugly.substring(loc + UGLY_SEPARATOR.length());
				return new User(email, authDomain, userId);
			}
		}
		return null;
	}

	public byte[] toBytes()
	{
		return Bytes.toBytes(email + UGLY_SEPARATOR + authDomain + UGLY_SEPARATOR + userId);
	}
}
